import com.alibaba.fastjson.{JSON, JSONObject}
import com.o2o.utils.Iargs
import com.o2o.utils.times.TimesYearAll
import org.apache.spark.sql.{Dataset, Row, SparkSession}

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/6/5 16:23
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object CheckInObsData {
  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name","O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", Iargs.OBSACCESS)
    sc.hadoopConfiguration.set("fs.s3a.secret.key", Iargs.OBSSECRET)
    sc.hadoopConfiguration.set("fs.s3a.endpoint", Iargs.OBSENDPOINT)
    sc.setLogLevel("WARN")

    //平台名称
    var platform = "dingdongmc"
    //当月的月份
    var month = 8
    var lastMonth = 7
    //每个月固定时间戳
    var year = 2020
    var timeStamp = TimesYearAll.TIME202008

    //MongoDB源数据路径
//    val sourcePath = s"s3a://o2o-sourcedata/obs-source-${year}/${month}/${platform}/${platform}_${year}_${month}/"
//
//    val index = "2020_dingdongmc_7/type_1"
//
//
//    var goodPath = s"s3a://o2o-dataproces-group/zsc/2020/8/intime/good_final_final/"
//
//
//    val frame: DataFrame = spark.read.orc(goodPath).select("brandValueId","brandName","firstCategoryId")
//
//    frame.repartition(1).write.json("D:/intime/brand/new")


    val goodPath="s3a://o2o-dataproces-group/zsc/2020/8/intime/good_final_modify_1/"

    val value: Dataset[Row] = spark.read.json(goodPath).where("firstCategoryId!='10019'")

    import org.elasticsearch.spark._

    value.toJSON.rdd.map(lines=>{
      val nObject: JSONObject = JSON.parseObject(lines)

      nObject
    }).saveToEs("2020_intime/intime_2020_8",Map("es.mapping.id"->"good_id"))


  sc.stop()
}
}
